import { HttpException, HttpStatus, Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { In, Repository } from 'typeorm';
import { Expense } from './expense.entity';
import { CreateDto } from './dto/create.dto'
import { RemoveDto } from './dto/remove.dto'
import { UpdateDto } from './dto/update.dto'
import { GetExpensesDto, expenseResponse } from './dto/get-expense.dto'

@Injectable()
export class ExpenseService {
  constructor(
    @InjectRepository(Expense)
    private expenseRepository: Repository<Expense>,
  ) { }

  async createExpense(createDto: CreateDto): Promise<{ isCreated: boolean, message: string, code: number }> {

    const newExpense = new Expense();
    newExpense.userId = createDto.userId;
    newExpense.billId = createDto.billId;
    newExpense.expenseDate = createDto.expenseDate;
    newExpense.amount = createDto.amount;
    newExpense.expenseType = createDto.expenseType;
    newExpense.attach = createDto.attach;
    newExpense.note = createDto.note;

    await this.expenseRepository.save(newExpense);



    return { isCreated: true, message: "添加支出成功!", code: 200 };
  }

  async getExpensesByBillId(getExpensesDto: GetExpensesDto): Promise<{ data: Array<expenseResponse>, message: string, code: number }> {
    if (!getExpensesDto.billId) {
      throw new HttpException('账单id不能为空!', HttpStatus.BAD_REQUEST);
    }
    const query = await this.expenseRepository.findAndCount({ where: { billId: getExpensesDto.billId, status: "1" } });
    let message = query[1] ? "查询成功!" : "根据账单id,查询结果为空!";
    const data = query[0].map(v => ({
      dataId: v.dataId,
      userId: v.userId,
      billId: v.billId,
      expenseDate: v.expenseDate,
      createdDate: v.createdDate,
      amount: v.amount,
      expenseType: v.expenseType,
      attach: v.attach,
      note: v.note
    }));

    return { data, message, code: 200 };
  }

  async removeExpensesByDataIds(removeDto: RemoveDto): Promise<{ isRemoved: boolean, message: string, code: number }> {
    if (!removeDto.dataIds?.length) {
      throw new HttpException('dataIds不能为空!', HttpStatus.BAD_REQUEST);
    }
    const removed = await this.expenseRepository.update({ dataId: In(removeDto.dataIds), status: "1" }, { status: "0" });
    if (removed.affected && removed.affected > 0) {
      return { isRemoved: true, message: removed.affected + "条记录, 删除成功!", code: 200 };
    } else {
      return { isRemoved: false, message: "删除失败!", code: 400 };
    }

  }

  async updateExpenseByDataId(updateDto: UpdateDto): Promise<{ isUpdated: boolean, message: string, code: number }> {
    if (!updateDto.dataId) {
      throw new HttpException('dataId不能为空!', HttpStatus.BAD_REQUEST);
    }
    const updated = await this.expenseRepository.update({ dataId: updateDto.dataId, status: "1" }, {
      billId: updateDto.billId,
      expenseDate: updateDto.expenseDate,
      amount: updateDto.amount,
      expenseType: updateDto.expenseType,
      attach: updateDto.attach,
      note: updateDto.note
    });
    if (updated.affected && updated.affected > 0) {
      return { isUpdated: true, message: "修改成功!", code: 200 };
    } else {
      return { isUpdated: false, message: "修改失败!", code: 400 };
    }

  }
}